package com.tfswx.ruleengine.fn;

import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.bean.DataObject;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/3/25
 */
public class GetValue extends BaseFn {

    public static Object getZdValue(DataObject.ZdItemDTO zd, boolean isWb, List<String> logs) {

        String zdmc = zd.getZdmc();

        String bgsj = zd.getBgsj();

        if (StringUtils.isNotBlank(bgsj)) {
            // 已赋值
            String bghzdwbz = zd.getBghzdwbz();
            String bghzdbmz = zd.getBghzdbmz();
            if (isWb && bghzdwbz != null) {
                logs.add("bgsj已赋值-" + zdmc + "获取变更后的文本值:" + bghzdwbz);
                return bghzdwbz;
            } else {
                logs.add("bgsj已赋值-" + zdmc + "获取变更后的编码值:" + bghzdbmz);
                return bghzdbmz;
            }
        } else {
            // 未赋值
            String bgqzdbmz = zd.getBgqzdbmz();
            String bgqzdwbz = zd.getBgqzdwbz();
            if (isWb && bgqzdwbz != null) {
                logs.add("bgsj未赋值-" + zdmc + "获取变更前的文本值:" + bgqzdwbz);
                return bgqzdwbz;
            } else {
                logs.add("bgsj未赋值-" + zdmc + "获取变更前的编码值:" + bgqzdbmz);
                return bgqzdbmz;
            }
        }
    }

    @Override
    public Object call(Object[] arguments, FelContext context) {
        DataObject dataObject = (DataObject) arguments[0];
        String arg = String.valueOf(arguments[1]);
        boolean isWb = false;
        if (arguments.length == 3) {
            isWb = (Boolean) arguments[2];
        }

        List<String> logs = service.getGzjyLog().getLogs();

        DataObject.ZdItemDTO zd = getZd(dataObject, arg);

        if (zd == null) {
            logs.add("未找到字段" + arg + "，返回:''");
            return "";
        }

        return getZdValue(zd, isWb, logs);
    }

    @Override
    public String getZwmc() {
        return "字段取值";
    }


    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("param1"));
        params.add(new Param("param2"));
        params.add(new Param("param3", "布尔值", false));
        return params;
    }

    @Override
    public String getName() {
        return "getValue";
    }
}
